package com.dtruth.dataflow.dao;

import java.util.List;
import java.util.Map;
import java.util.Set;

import com.dtruth.dataflow.dbmanager.RedisManager;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;

public class RedisDAO {

	private Jedis jedis = null;

	public RedisDAO(String host, int port, int insNum) {
		jedis = new RedisManager(host, port, insNum).getJedis();
	}

	public Jedis getJedis() {
		return jedis;
	}

	public void setJedis(Jedis jedis) {
		this.jedis = jedis;
	}

	/**
	 * 首部提取
	 * 
	 * @return
	 */
	public String blpop() {
		return jedis.blpop(0, "queue:crawled").get(1);
	}

	/**
	 * 尾部插入
	 * 
	 * @param value
	 */
	public long rpush(String value) {
		return jedis.rpush("queue:crawled", value);
	}

	public boolean isExist4Set(String key, String smember) {
		return jedis.smembers(key).contains(smember);
	}

	public Long sadd(String key, String smember) {
		return jedis.sadd(key, smember);
	}

	/**
	 * 头部插入
	 * 
	 * @param value
	 */
	public long lpushx(String value) {
		return jedis.lpushx("queue:crawled", value);
	}

	public synchronized String setWithTTL(String key, String value) {
		return jedis.set(key, value, "NX", "EX", 60 * 60 * 24 * 7L);
	}

	public Set<String> getKeys(String pattern) {
		return jedis.keys(pattern);
	}

	public String get(String key) {
		return jedis.get(key);
	}

	/**
	 * 重复次数累计
	 * 
	 * @param key
	 * @param bi_content
	 * @return
	 */
	public synchronized List<Object> hincrOrHset(String key, String bi_title,
			String bi_content, String score) {
		Transaction tx = jedis.multi();
		if (tx.exists(key) != null) {
			if (bi_title != null)
				tx.hset(key, "title", bi_title);
			tx.hset(key, "content", bi_content);
			tx.hincrBy(key, "counter", 1);
			tx.hincrBy(key, "score", Long.parseLong(score));
		} else {
			tx.hmget(key, "title", bi_title, "content", bi_content, "counter",
					"1", "score", score);
		}
		tx.expire(key, 60 * 60 * 24 * 7);
		return tx.exec();
	}

	public void hincrOrHset4Tmp(String key, String score) {
		if (jedis.exists(key) != null) {
			jedis.hincrBy(key, "quality", Long.parseLong(score));
		}
	}

	public synchronized String hget(String key, String field) {
		return jedis.hget(key, field);
	}

	public Long hset(String key, String field, String value) {
		return jedis.hset(key, field, value);
	}

	public String hmset(String key, Map<String, String> values) {
		return jedis.hmset(key, values);
	}

	public Long del(String key) {
		return jedis.del(key);
	}

	public Map<String, String> hgetAll(String key) {
		return jedis.hgetAll(key);
	}

	public synchronized List<Object> zincrby(String key, double score,
			String member, int expireTime) {
		Transaction tx = jedis.multi();
		tx.zincrby(key, score, member);
		tx.expire(key, expireTime);
		return tx.exec();
	}

	public Set<String> zrangeByScore(String key, double min, double max) {
		return jedis.zrangeByScore(key, min, max);
	}

	public Long zrem(String key, String members) {
		return jedis.zrem(key, members);
	}

	public Long zrank(String key, String member) {
		return jedis.zrank(key, member);
	}

	/**
	 * 加入set
	 * 
	 * @param key
	 * @param members
	 * @return
	 */
	public synchronized List<Object> saddWithTTL(String key, String members) {
		Transaction tx = jedis.multi();
		tx.sadd(key, members);
		tx.expire(key, 60 * 60 * 24 * 7);
		return tx.exec();
	}

	/**
	 * 读取set
	 * 
	 * @param key
	 * @return
	 */
	public synchronized Set<String> smembers(String key) {
		return jedis.smembers(key);
	}
}
